package main

import (
	"fmt"
	"reflect"
)

type Myint int

type Person struct {
	Name string
	Age  string
}

// 反射获取任意变量的类型
func reflectFn(x interface{}) {
	v := reflect.TypeOf(x)
	// 类型种类, 底层的类型
	fmt.Printf("类型=%v, 类型名称:%v, 类型种类:%v\n", v, v.Name(), v.Kind())
}

func reflectValue(x interface{}) {
	//b, _ := x.(int)
	//fmt.Println(b + 10)

	// 反射功能实现
	//v := reflect.ValueOf(x)
	//// cannot convert 10 (untyped int constant) to type struct{typ_ *abi.Type; ptr unsafe.Pointer; reflect.flag}
	//fmt.Println(v + 10)

	// 反射获取变量的原始值
	v := reflect.ValueOf(x)
	var m = v.Int() + 10
	fmt.Println(m)

	kind := v.Kind()
	switch kind {
	case reflect.Int:
		fmt.Println("int")
	case reflect.Float64:
		fmt.Println("Float64")
	case reflect.Int64:
		fmt.Println("int64")
	case reflect.String:
		fmt.Println("string")
	default:
		panic("unhandled default case")
	}
}

func main() {
	//reflectFn(123)
	//reflectFn(123.123)
	//reflectFn(true)
	//reflectFn("hello")
	//
	//var e Myint = 34
	//reflectFn(e)
	//
	//var p = Person{
	//	Name: "张三",
	//	Age:  "18",
	//}
	//reflectFn(p)
	//
	//var h = 25
	//// 类型=*int, 类型名称:, 类型种类:ptr
	//reflectFn(&h)
	//var s = [3]int{1, 2, 3}
	////类型=[3]int, 类型名称:, 类型种类:array
	//reflectFn(s)
	//var s2 = []string{"hello", "world"}
	////类型=[]string, 类型名称:, 类型种类:slice
	//reflectFn(s2)

	// reflect value
	var a int = 123
	reflectValue(a)
}
